<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */
defined('IN_DOUCO_API') or exit('Access Denied');


//单例模式生成加载器
function load() {
    static $loader;
    if(empty($loader)) {
        $loader = new Loader();
    }
    return $loader;
}
class Loader{
    private $cache = array();


    private $library_map = array(
        'agent' => 'agent/agent.class',
        'captcha' => 'captcha/captcha.class',
        'pdo' => 'pdo/PDO.class',
        'qrcode' => 'qrcode/phpqrcode',
        'ftp' => 'ftp/ftp',
        'pinyin' => 'pinyin/pinyin',
        'pkcs7' => 'pkcs7Encoder',
    );

    /**
     * 加载函数
     * @param $name
     * @return bool
     */
    function func($name){
        if (isset($this->cache['func'][$name])) {
            return true;
        }
        $file = LOADER_PATH . 'func/' . $name . '.func.php';
        if (file_exists($file)) {
            include $file;
            $this->cache['func'][$name] = true;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 加载类
     * @param $name
     * @return bool
     */
    function classs($name) {

        if (isset($this->cache['class'][$name])) {
            return true;
        }
        $file = LOADER_PATH . 'class/' . $name . '.class.php';
        if (file_exists($file)) {
            include $file;
            $this->cache['class'][$name] = true;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 加载第三方库
     * @param $name
     * @return bool
     */
    function library($name) {
        if (isset($this->cache['library'][$name])) {
            return true;
        }
        if (!empty($this->library_map[$name])) {
            $name = $this->library_map[$name];
        }
        $file = LOADER_PATH . 'library/' . $name . '.php';
        if (file_exists($file)) {
            include $file;
            $this->cache['library'][$name] = true;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 加载model
     * @param $name
     * @return bool
     */
    function model($name) {
        if (isset($this->cache['model'][$name])) {
            return true;
        }
        $file = LOADER_PATH . 'model/' . $name . '.mod.php';
        if (file_exists($file)) {
            include $file;
            $this->cache['model'][$name] = true;
            return true;
        } else {
            trigger_error('Invalid Model /include/model/' . $name . '.mod.php', E_USER_ERROR);
            return false;
        }
    }
}
